<template>
  <div class="changPossWord">
    <el-dialog
      title="添加用户"
      :visible.sync="changPossWord"
      width="35%"
      :before-close="passWordCancel"
    >
      <el-form
        :model="newPassWord"
        status-icon
        :rules="rules"
        ref="newPassWord"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号:">
          <div>{{ formDate.userName }}</div>
        </el-form-item>
        <el-form-item label="原密码" prop="oldPassWord">
          <el-input
            type="password"
            v-model="newPassWord.oldPassWord"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="newPassWord.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="newPassWord.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="passWordCancel">取 消</el-button>
        <el-button type="primary" @click="savePassWord('newPassWord')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
// 加密本地存储
import CryptoJS from "crypto-js";

import { updatePwd } from "../api/user";
export default {
  name: "ChangPossWord",
  props: ["changPossWord"],
  data() {
    // 校验新密码是否一致
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.newPassWord.checkPass !== "") {
          this.$refs.newPassWord.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.newPassWord.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      formDate: JSON.parse(
        CryptoJS.AES.decrypt(
          sessionStorage.getItem("ruleForm"),
          "secretkey"
        ).toString(CryptoJS.enc.Utf8)
      ),
      newPassWord: {
        oldPassWord: "",
        pass: "",
        checkPass: "",
      },
      rules: {
        oldPassWord: [
          { required: true, message: "请输入原密码", trigger: "change" },
        ],
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 取消修改密码
    passWordCancel() {
      (this.newPassWord = { oldPassWard: "", pass: "", checkPass: "" }),
        this.$emit("passWordCancel", false);
    },
    // 确认修改密码
    savePassWord(formName) {
      var pwd = JSON.parse(
        CryptoJS.AES.decrypt(
          sessionStorage.getItem("pwd"),
          "secretkey"
        ).toString(CryptoJS.enc.Utf8)
      );
      if (this.newPassWord.oldPassWord !== pwd) {
        this.$message.error("原密码错误");
      } else {
        let updatePsw = {
          newPwd: this.newPassWord.checkPass,
          oldPwd: this.newPassWord.oldPassWord,
          userName: this.formDate.userName,
        };
        updatePwd(updatePsw).then((res) => {
          if (res.status === 200) {
            this.$store
              .dispatch("user/logout", this.loginForm)
              .then(() => {
                this.$router.push({ path: "/login" });
              })
              .catch(() => {});
            this.$message({
              message: `密码修改成功`,
              type: "success",
            });
          }
        });
        this.$emit("savePassWord", false);
        this.newPassWord = {
          oldPassWord: "",
          pass: "",
          checkPass: "",
        };
      }
    },
  },
};
</script>
<style lang="scss" scoped></style>
